#include <algorithm>
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {
    int beta = 2, p = 3, L = -1, U = 1;

    // (1) 计算 UFL 和 OFL
    cout << "(1)" << endl;
    double UFL = pow(beta, L);
    double OFL = pow(beta, U) * (beta - pow(beta, 1 - p));
    cout << "UFL=" << UFL << endl;
    cout << "OFL=" << OFL << endl;

    // (2) 枚举所有数
    cout << "(2)" << endl;
    ofstream B2("B2.txt");

    B2 << "all numbers in F:" << endl;
    B2 << "二进制" << " , " << "十进制" << endl;
    B2 << "0.00" << " , " << "0" << endl;

    int count = 1;
    for (int e = L; e <= U; e++) {
        for (int i = 0; i <= 1; i++) {
            for (int j = 0; j <= 1; j++) {
                double positiveVal = (1.0 + 0.5 * i + 0.25 * j) * pow(beta, e);
                double negativeVal = -positiveVal;
                B2 << (e < 0 ? "+" : "") << "1." << i << j << (e < 0 ? "×2^(" : "×2^") << e << ") , " << positiveVal << endl;
                B2 << (e < 0 ? "-" : "") << "1." << i << j << (e < 0 ? "×2^(" : "×2^") << e << ") , " << negativeVal << endl;
                count += 2;
            }
        }
    }
    int number = pow(2, p) * (U - L + 1) + 1;
    B2 << "count=" << count << endl;
    B2 << "#F=" << number << endl;

    B2.close();

    cout << "the number of numbers in F:count=" << count << endl;
    cout << "the cardinality of F:#F=" << number << endl;
    cout << "count=#F" << endl;

    // (4) 枚举所有次正规数
    cout << "(4)" << endl;
    ofstream B4("B4.txt");

    B4 << "all the subnormal numbers of F:" << endl;
    B4 << "二进制" << " , " << "十进制" << endl;

    int subcount = 0;
    for (int i = 0; i <= 1; i++) {
        for (int j = 0; j <= 1; j++) {
            if (!(i == 0 && j == 0)) {
                double a = (0.5 * i + 0.25 * j) * pow(beta, L);
                double b = -a;
                B4 << "+0." << i << j << "×2^(-1)" << " , " << a << endl;
                B4 << "-0." << i << j << "×2^(-1)" << " , " << b << endl;
                subcount += 2;
            }
        }
    }
    B4 << "subcount=" << subcount << endl;

    B4.close();

    cout << "the number of subnormal numbers of F:subcount=" << subcount << endl;


    return 0;
}
